<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php $message = ""; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
  
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){
//$bool=verifLibelle1('Code_condition_indemnisation') &&  verifLibelle('Libelle_condition_indemnisation') && verifLibelle('Identifiant_offre') && verifNumeric($_POST['Valeur_condition_indemnisation']) && dateValide('Date_debut_condition_indemnisation');
$bool=verifLibelle1('Code_condition_indemnisation') &&  verifLibelle('Libelle_condition_indemnisation') && verifLibelle('Identifiant_offre') && dateValide('Date_debut_condition_indemnisation');
	if(!empty($_POST['Date_fin_condition_indemnisation']))$bool=$bool && dateValide($_POST['Date_fin_condition_indemnisation']);

//$bool1=verifNumeric($_POST['Valeur_condition_indemnisation']);}

if(isset($_POST["MM_insert"])&& !$bool) 
{

if(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_debut_condition_indemnisation']){
	$datedebut = datevalider($_POST['Date_debut_condition_indemnisation']);
		$datefin = datevalider($_POST['Date_fin_condition_indemnisation']);
	$datevalide1 = datevalide($_POST['Date_debut_condition_indemnisation']);
		$datevalide2 = datevalide($_POST['Date_fin_condition_indemnisation']);
	$lesdates=verifDate($_POST['Date_debut_condition_indemnisation'],$_POST['Date_fin_condition_indemnisation']);
}

// --- si le code existe pour l'offre
$toCriterias['Code_condition_indemnisation'] = $_POST['Code_condition_indemnisation'] ;
$toCriterias['Identifiant_garantie'] = $_POST['Identifiant_garantie'] ;
$existeGarantie = existeEnregistrement($database_microassur, $microassur, "conditionindemnisation", $toCriterias) ;

// --- si le code existe pour le produit
$toCriterias['Identifiant_offre'] = $_POST['Identifiant_offre'] ;
unset($toCriterias['Identifiant_garantie']) ;
$existeOffre = existeEnregistrement($database_microassur, $microassur, "conditionindemnisation", $toCriterias) ;

$nums = $existeGarantie && $existeOffre ;
// $nums=existeEnregistrement($database_microassur, $microassur,"conditionindemnisation",$toCriterias);
// if(isset($_POST["MM_insert"]) && existeChamp($database_microassur, $microassur,'conditionindemnisation','Code_condition_indemnisation',@$_POST['Code_condition_indemnisation']) )
if( isset($_POST["MM_insert"]) && $existeGarantie!=0){
	$message = colorer("Cette condition indemnisation existe déjà pour la garantie choisie","false");
}elseif($existeOffre!=0){
	$message = colorer("Cette condition indemnisation existe déjà pour l'offre choisie","false");
}
elseif(empty($_POST['Code_condition_indemnisation'])) $message = colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_condition_indemnisation')) $message = colorer("Le code saisi comporte des caractères spéciaux interdits ","false");

elseif(empty($_POST['Libelle_condition_indemnisation'])) $message = colorer(" Vous devez saisir le libellé","false");
elseif(!verifLibelle('Libelle_condition_indemnisation')) $message = colorer("Le libellé saisi comporte des caractères spéciaux interdits ","false");
// --- elseif(empty($_POST['Valeur_condition_indemnisation'])) $message = colorer(" Vous devez saisir la valeur de la condition d’indemnisation","false");
// elseif(!verifNumeric($_POST['Valeur_condition_indemnisation'])) $message = colorer("La valeur de la condition d’indemnisation doit être numérique","false");

elseif(empty($_POST['Date_debut_condition_indemnisation'])) $message = colorer("Vous devez saisir la date de début","false");

elseif(isset($_POST['Date_debut_condition_indemnisation']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_condition_indemnisation']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_fin_condition_indemnisation']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_fin_condition_indemnisation']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}
elseif(isset($_POST['Date_debut_condition_indemnisation']) && isset($_POST['Date_fin_condition_indemnisation'])  && !empty($_POST['Date_fin_condition_indemnisation']) && !empty($_POST['Date_debut_condition_indemnisation'])&& $lesdates==FALSE)
	{$message = colorer("La date de fin doit être postérieure à la date de début","false"); }
	else
		{
		  $insertSQL = sprintf("INSERT INTO conditionindemnisation (Identifiant_condition_indemnisation, Code_condition_indemnisation, Libelle_condition_indemnisation, Valeur_condition_indemnisation, Type_condition_indemnisation, Date_debut_condition_indemnisation, Date_fin_condition_indemnisation, Identifiant_garantie, Identifiant_offre) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
							   GetSQLValueString($_POST['Identifiant_condition_indemnisation'], "int"),
							   GetSQLValueString($_POST['Code_condition_indemnisation'], "text"),
							   GetSQLValueString($_POST['Libelle_condition_indemnisation'], "text"),
							   GetSQLValueString($_POST['Valeur_condition_indemnisation'], "int"),
							   GetSQLValueString($_POST['Type_condition_indemnisation'], "text"),
							   GetSQLValueString(dateEN2FR($_POST['Date_debut_condition_indemnisation']), "date"),
							   GetSQLValueString(dateEN2FR($_POST['Date_fin_condition_indemnisation']), "date"),
							   GetSQLValueString($_POST['Identifiant_garantie'], "int"),
							   GetSQLValueString($_POST['Identifiant_offre'], "int"));

		  mysql_select_db($database_microassur, $microassur);

		 mysql_query("START TRANSACTION");

		  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
		  
				if($Result1) { mysql_query("commit");header("location:index.php?ajout=success"); }
				else { mysql_query("rollback"); $message = colorer("Contacter la DSI, erreur création condition d'indémnisation","false");}
		}
	}
}
mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM conditionindemnisation";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rsgaranti = "SELECT * FROM garantie";
$rsgaranti = mysql_query($query_rsgaranti, $microassur) or die(mysql_error());
$row_rsgaranti = mysql_fetch_assoc($rsgaranti);
$totalRows_rsgaranti = mysql_num_rows($rsgaranti);

mysql_select_db($database_microassur, $microassur);
$query_rs_offre = "SELECT * FROM offre";
$rs_offre = mysql_query($query_rs_offre, $microassur) or die(mysql_error());
$row_rs_offre = mysql_fetch_assoc($rs_offre);
$totalRows_rs_offre = mysql_num_rows($rs_offre);
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0003"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);

?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']='success') $message = colorer("La condition d'indemnisation est créée","true"); ?>
<?php if(isset($_GET['mod']) && $_GET['mod']=='success') $message = colorer("La modification de l'utilisateur est prise en compte","true"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condition.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Condition indemnisation";
					$nommodule2="Liste conditions indemnisation";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1 btnr"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
			
			
			<table class="tab_list">
						  <tr id="niv1">
							<td class="cod_cidem">CODE</td>
							<td class="lib_cidem">LIBELLE</td>
							<td class="date_cidem">DATES DEBUT ET FIN</td>
							<td class="val_cidem">VALEUR</td>
							<td class="type_cidem">TYPE</td>
							<td class="gar_cidem">GARANTIE</td>
							<td class="ofr_cidem">OFFRE</td>
						  </tr>
			</table>			
				<div class="content_table">
					<table class="tab_list" id="el1">
						   <?php $i=1; do {
						  $col_rs_garantie_liste = "-1";
						if (isset($row_rs['Identifiant_garantie'])) {
						  $col_rs_garantie_liste = $row_rs['Identifiant_garantie'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_garantie_liste = sprintf("SELECT * FROM garantie WHERE Identifiant_garantie = %s", GetSQLValueString($col_rs_garantie_liste, "int"));
						$rs_garantie_liste = mysql_query($query_rs_garantie_liste, $microassur) or die(mysql_error());
						$row_rs_garantie_liste = mysql_fetch_assoc($rs_garantie_liste);
						$totalRows_rs_garantie_liste = mysql_num_rows($rs_garantie_liste);

						$col1_rs_offre_liste = "-1";
						if (isset($row_rs['Identifiant_offre'])) {
						  $col1_rs_offre_liste = $row_rs['Identifiant_offre'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_offre_liste = sprintf("SELECT * FROM offre WHERE Identifiant_offre = %s", GetSQLValueString($col1_rs_offre_liste, "int"));
						$rs_offre_liste = mysql_query($query_rs_offre_liste, $microassur) or die(mysql_error());
						$row_rs_offre_liste = mysql_fetch_assoc($rs_offre_liste);
						$totalRows_rs_offre_liste = mysql_num_rows($rs_offre_liste);
						   ?>
						  <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="cod_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo $row_rs['Code_condition_indemnisation']; ?></a></td>
							  <td class="lib_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo tronque($row_rs['Libelle_condition_indemnisation'], 17); ?></a></td>
							  <td class="date_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo dateEN2FR($row_rs['Date_debut_condition_indemnisation']); ?> / <?php if($row_rs['Date_fin_condition_indemnisation']!=NULL) echo dateEN2FR($row_rs['Date_fin_condition_indemnisation']); else echo $indefini; ?></a></td>
							  <td class="val_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo $row_rs['Valeur_condition_indemnisation']; ?></a></td>
							  <td class="type_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo $row_rs['Type_condition_indemnisation']; ?></a></td>
							  <td class="gar_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo tronque($row_rs_garantie_liste['Code_garantie'], 9); ?></a></td>
							  <td class="ofr_cidem"><a href="modconditionindemnisation.php?Identifiant_condition_indemnisation=<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>"><?php echo tronque($row_rs_offre_liste['Code_offre'], 9); ?></a></td>
							</tr>
							<?php } // Show if recordset not empty ?>

						  <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
					</table>
				</div>
			</div>
		<div class="imgright"></div>
			<div class="cleared"></div>
		<!--/***************************************/-->
			<div id="br"></div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
				<div class="imgmiddle">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
						<div class="content_tableform1">
						  <table>
							<tr>
							  <td>CODE</td>
							  <td><input class="resize_1" type="text" maxlength="5" name="Code_condition_indemnisation" value="<?php if(isset($_POST['Code_condition_indemnisation']))echo remind_text('Code_condition_indemnisation'); ?>" size="32" /></td>
							</tr>
							<tr>
							  <td>LIBELLE</td>
							  <td colspan="3"><input class="td2" type="text" name="Libelle_condition_indemnisation" value="<?php if(isset($_POST['Libelle_condition_indemnisation']))echo remind_text('Libelle_condition_indemnisation'); ?>" size="32" maxlength="30"/></td>
							</tr>
							<tr>
							  <td>VALEUR</td>
							  <td colspan="3"><input class="td1" type="text" name="Valeur_condition_indemnisation" value="<?php if(isset($_POST['Valeur_condition_indemnisation']))echo remind_text('Valeur_condition_indemnisation'); ?>" size="32"  maxlength="10"/></td>
							</tr>
							<tr>
							  <td>DATE DEBUT</td>
							  <td><input type="text" name="Date_debut_condition_indemnisation" class="date" id="from" value="<?php if(isset($_POST['Date_debut_condition_indemnisation']))echo $_POST['Date_debut_condition_indemnisation']?>" size="32"  maxlength="10"/> </td>
							  <td class="td">DATE FIN</td>
							  <td><input type="text" name="Date_fin_condition_indemnisation" class="date" id="to" value="<?php if(isset($_POST['Date_fin_condition_indemnisation']))echo $_POST['Date_fin_condition_indemnisation']?>" size="32"  maxlength="10"/> </td>
							</tr>
							<tr>
							  <td>TYPE</td>
							  <td colspan="3"><select class="select" name="Type_condition_indemnisation">
								<?php do{ ?>
											<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_condition_indemnisation', $row_rs_dico2['Code_element']) ?>><?php echo $row_rs_dico2['Nom_element']; ?></option>
								<?php 	} while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
								</select>
							  </td>
							</tr>
							<tr>
							  <td>GARANTIE</td>
							  <td><select name="Identifiant_garantie">      <option value="">Sélectionner une garantie</option>
								<?php
						do {  
						?>
								<option value="<?php echo $row_rsgaranti['Identifiant_garantie']?>"><?php echo $row_rsgaranti['Libelle_garantie']?></option>
								<?php
						} while ($row_rsgaranti = mysql_fetch_assoc($rsgaranti));
						  $rows = mysql_num_rows($rsgaranti);
						  if($rows > 0) {
							  mysql_data_seek($rsgaranti, 0);
							  $row_rsgaranti = mysql_fetch_assoc($rsgaranti);
						  }
						?>
							  </select>      </td>

							  <td class="td">OFFRE</td>
							  <td><select name="Identifiant_offre">
								<?php
						do {  
						?>
								<option value="<?php echo $row_rs_offre['Identifiant_offre']?>"><?php echo $row_rs_offre['Libelle_offre']?></option>
								<?php
						} while ($row_rs_offre = mysql_fetch_assoc($rs_offre));
						  $rows = mysql_num_rows($rs_offre);
						  if($rows > 0) {
							  mysql_data_seek($rs_offre, 0);
							  $row_rs_offre = mysql_fetch_assoc($rs_offre);
						  }
						?>
									</select></td>
							</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
						
  <input type="hidden" name="Identifiant_condition_indemnisation" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
		</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
mysql_free_result($rsgaranti);
mysql_free_result($rs_offre);
mysql_free_result($rs_garantie_liste);
mysql_free_result($rs_offre_liste);
?>
